﻿using QFramework;
using UnityEngine;
using HKTools;
using UnityEngine.UI;

public class Test_Stopwatch : MonoBehaviour, IController
{
    public IArchitecture GetArchitecture()
    {
        return HK_ArchitectureProvider.Architecture;
    }

    [Header("UI Controls")]
    [SerializeField] Button btn_CreateStopwatch; // 创建新计时器
    [SerializeField] Button btn_Start;          // 开始计时
    [SerializeField] Button btn_Pause;          // 暂停计时
    [SerializeField] Button btn_Resume;         // 恢复计时
    [SerializeField] Button btn_Stop;           // 停止并重置
    [SerializeField] Button btn_Restart;        // 重新开始
    [SerializeField] Button btn_CleanAllStopwatch;        // 重新开始

    [Header("Display")]
    [SerializeField] Text txt_RemainingTime;    // 剩余时间显示文本

    // 当前活动的倒计时实例
    HK_Stopwatch stopwatch;

    void Awake()
    {
        // 初始化按钮交互逻辑
        btn_CreateStopwatch.onClick.AddListener(() =>
        {
            // 创建新实例前清理旧计时器
            stopwatch?.Stop();
            stopwatch = null;

            // 创建30秒倒计时（测试用例）
            stopwatch = this.SendCommand(new CMD_CreateStopwatch(30, () =>
            {
                HKDebug.Log("倒计时结束");
            }));

            // 绑定剩余时间显示（带初始值注册）
            stopwatch.RemainingTime.RegisterWithInitValue((value) =>
            {
                txt_RemainingTime.text = $"剩余时间：{value:F1}秒";
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
        });

        btn_Start.onClick.AddListener(() =>
        {
            stopwatch?.Start();
        });

        btn_Pause.onClick.AddListener(() =>
        {
            stopwatch?.Pause();
        });

        btn_Resume.onClick.AddListener(() =>
        {
            stopwatch?.Resume();
        });

        btn_Restart.onClick.AddListener(() =>
        {
            stopwatch?.Restart();
        });

        btn_Stop.onClick.AddListener(() =>
        {
            stopwatch?.Stop();
        });

        btn_CleanAllStopwatch.onClick.AddListener(() =>
        {
            this.SendCommand(new CMD_CleanAllStopwatch());
        });
    }
}
